let Q = require('q');
let defer = Q.defer();

var users = [{ 'name': 'andrew', 'passwd': 'password' }];

function getUser(username) {
    var user;
    users.forEach(function(element) {
        if (element.name === username) {
            user = element;
        }
    });
    return user;
}
/**
 * 获取初始promise
 * @private
 */

function getInitialPromise() {
    return defer.promise;
}

/**
 * 为promise设置三种状态的回调函数
 */
// var outputPromise = getInitialPromise().then(
//     (success) => {
//         return 'success';
//     }, (err) => {
//         //抛出异常
//         throw new Error('rejected');
//     }, (progress) => {
//         return 'progress';
//     });

// outputPromise.then(
//     (resolve) => {
//         console.log('resolve:' + resolve)
//     }, (reject) => {
//         console.log('reject:' + reject)
//     })
getInitialPromise().then((username) => {
    return getUser(username)
}).then((user) => {
    console.log(user)
}).catch((err) => {
    console.log(err);
})

defer.reject('andrews');
//defer.reject();